package DB

import (
	"github.com/RaymondCode/simple-demo/DB/table"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

var dsn = "wh:whwytgmwmdzd@(localhost:3306)/douyin?charset=utf8&parseTime=True&loc=Local"

var DB *gorm.DB

func Init() *gorm.DB {
	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, //关闭表名复数
		},
		SkipDefaultTransaction: true,                                //禁止只能单次增删改
		PrepareStmt:            true,                                //开启在缓存语句中执行给定的查询
		Logger:                 logger.Default.LogMode(logger.Info), //设置日志级别为 4
	})
	if err != nil {
		panic("Failed to connect database")
	}
	DB.AutoMigrate(&table.User{}, &table.Video{})
	return DB
}
